package com.shan.mybatis.service.impl;

import com.shan.mybatis.mapper.DepMapper;
import com.shan.mybatis.mapper.EmpMapper;
import com.shan.mybatis.pojo.Dept;
import com.shan.mybatis.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DepMapper mapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {
        return mapper.list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) {
        mapper.deleteById(id);
        empMapper.deleteByDeptId(id);
    }

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        mapper.addDept(dept);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        mapper.update(dept);
    }

    @Override
    public Dept selectByid(Integer id) {
        Dept list = mapper.selectById(id);
        return list;
    }
}
